﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Database;
using System.Data;
using System.Text.RegularExpressions;

namespace Userslib
{
    /*
     * All the details of a player will be hold here.
     * His statistics, name , alliance, units and race
     * As well as the commandlist with the commands he is allowed to execute.
     * implements IComparable and IEquatable<String> to make it possible to compare with another instance or name
     */
    public class User : IComparable, IEquatable<String>
    {
        private static Dictionary<String,int> MemberList ;
        private static Dictionary<String, String> MemberTribe ;
        private static Dictionary<String,int> CommandList ;
        private static String Ally = "FF";
        private Units UnitList;

        protected String username;
        public String Username { get { return this.username; } set { this.username = value; } }

        protected int accesslvl;
        public  int Accesslvl { get { return this.accesslvl; } }

        protected int pop;
        public  int Population { get { return this.pop; } }

        protected String greeting;
        public  String Greeting { get { return this.greeting; } }

        protected bool UserExists = false;
        public bool ValidUser { get { return UserExists; } }

        protected String race;
        public String Race { get { return this.race; } }

        public User(String Member)
        {
            this.username = Member;
           
            if (MemberList == null)
                UpdateUsers();
            if (isUser(Member))
            {
                UserExists = true;
                
                GetData(Member);
                GetCommands();
                this.UnitList = new Units(this.Race);
            }
            
        }

        public void GetData(String User)
        {
            try
            {
                if (MemberList == null)
                    UpdateUsers();
                if (CommandList == null)
                    GetCommands();
                User = User[0].ToString().ToUpper() + User.Substring(1);
                this.UserExists = MemberList.ContainsKey(User);
                if (this.UserExists)
                {
                    this.race = MemberTribe.Where(x => x.Key == User).FirstOrDefault().Value;
                    this.pop = MemberList.Where(x =>  x.Key == User).FirstOrDefault().Value;
                    this.accesslvl = 3;
                }
                else
                {
                    this.race = null;
                    this.pop = 0;
                    this.accesslvl = 0;
                }
            }
            catch (Exception Exc)
            {
                throw Exc;
            }
        }
        /*
         * Update the dictionary containg the members of the alliance
         */
        public static void UpdateUsers()
        {
            localhost.GetData Ws = new localhost.GetData();
            
            var m = from localhost.Users member in Ws.Get_Members(Ally) orderby member.Pop select member ;
            MemberList = m.ToDictionary(x => x.Name, x => x.Pop);
            MemberTribe = m.ToDictionary(x => x.Name, x => Races.getRace(x.Tribe));            
        }
        /*
         * Update the dictionary containg the commands.
         */
        private void GetCommands()
        {
            localhost.GetData Ws = new localhost.GetData();
            var commands = from localhost.Commands command in Ws.Get_Commands(this.accesslvl) where command.Access_lvl <= this.accesslvl select command;
            CommandList = commands.ToDictionary(x => x.Command_Name, x => x.Access_lvl);
        }

        /*
         * Determine wether a given user is a member or not
         */
        public static bool isUser(String Name)
        {
            if (MemberList == null)
                UpdateUsers();
            try
            {   
                Name = char.ToUpper(Name[0]) + Name.Substring(1);

                return MemberList.ContainsKey(Name);
            }
            catch
            {
                return false;
            }           
        }

        public void Update_Commands()
        {
            GetCommands();
        }

        public Dictionary<String, int> GetTroops()
        {
            return this.UnitList.GetSpeedUnits();
        }
        public Dictionary<String,int> getAttackingTroops(){
            return this.UnitList.getAttackingUnits();
        }
        /*
         * Determine wether or not a user is allowed to use a certain command
         */
        public bool Allowed(String Command)
        {
            try
            {                
                return CommandList.ContainsKey(Command);
            }
            catch (Exception Exc)
            {
                throw Exc;
                
            }
        }
        
        public override string ToString()
        {
            return String.Concat(this.Username , ". Has an accesslevel of : " , this.Accesslvl);
        }

        public override bool Equals(System.Object obj)
        {
            if (obj is User)    
                return System.String.Equals(this.ToString(),obj.ToString());
            if (obj is String)
                return this.Username.Equals(obj.ToString());
            return false;
        }
        public bool Equals(User Memberobj)
        {
            return this.username.Equals(Memberobj.username);
        }

        public bool Equals(String MemberName)
        {
            return this.Username.Equals(MemberName);
        }
        int IComparable.CompareTo(object obj)
        {
            String Username = (String)obj;
            return String.Compare(this.Username, Username);

        }


        bool IEquatable<string>.Equals(string other)
        {
            return String.Equals(this.Username, other);
        }
    }

}
